<?php
if(!defined('DREAMSCAPE')){die('Sorry, but this file cannot be directly viewed.');} 
/*   Comments   //-------*/
add_admin_menu(L_ADMIN_MANAGE_COMMENTS,array('<a href="?cat=admin&amp;sub=comments">'.L_MENU_EDIT.'</a>'),'comments');

if($admin_sub !== 'comments'){return;}

$items = isset($items) && is_object($items) ? $items : new Dreamscape('items');
$comments = isset($comments) && is_object($comments) ? $comments : new Dreamscape('comments');

	if(empty($item_id)){
	add_breadcrumb(L_COMMENT_EDIT_TITLE);
	add_title(L_COMMENT_EDIT_TITLE);
	} else {
	add_breadcrumb('<a href="index.php?cat=admin&sub=comments">'.L_COMMENT_EDIT_TITLE.'</a>');
	
	$comments->Get($item_id);
	$comment_title = !empty($comments->name) ? $comments->name : L_NO_TEXT_IN_TITLE;
	add_breadcrumb(sprintf(L_CURRENTLY_EDITING, $comment_title));
	add_title(sprintf(L_CURRENTLY_EDITING, $comment_title));
	}

ozone_action('admin_page', 'comments_content');
function comments_content(){
global $output, $comments, $items, $item_id;
if(is_posting(L_BUTTON_DELETE)){
	if(isset($_POST['del'])){
				$del = check_array($_POST['del']);
				foreach($del as $id){
					$ct = $comments;
					$ct->Get($id);
					$name = empty($ct->name) ? L_COMMENT_NAME_MISSING : $ct->name;
					if(deleteItem($id, 'comments')){
					$result[] = '<li>'.sprintf(L_COMMENT_DELETE_SUCCESS,$name).'</li>';
					} else {
					if(empty($ct->id)){
					$result[] = '<li>'.L_COMMENT_MISSING.'</li>';
					} else {
					$result[] = '<li>'.sprintf(L_COMMENT_DELETE_FAILURE,$name).'</li>';
					}
					
					}
				}
				$ips_to_ban = array();
				if(isset($_POST['ban_delete'])){
				$ip = check_array($_POST['ip']);
				$banned_ips = getOption('bannedips');
				$banned_ips = $banned_ips == false ? '' : $banned_ips;
				
					foreach($ip as $k => $the_ip){
					if(!isset($del[$k]) || strpos($banned_ips, $the_ip) !== false || in_array($the_ip,$ips_to_ban)){continue;}
					$ips_to_ban[] = $the_ip;
					
					}
				$ips_to_ban_proper = proper_list($ips_to_ban);
				$ips_to_ban = (empty($banned_ips) ? '' : $banned_ips.',').implode(',',$ips_to_ban);
				
				}
				
				$result = '<ul>'.implode("\n", $result).'</ul>';
				printOut(SUCCESS, $result);
				if(!empty($ips_to_ban) && !empty($ips_to_ban_proper)){
					if(setOption('bannedips', $ips_to_ban)){
						printOut(SUCCESS, sprintf(L_COMMENT_IPS_BANNED, $ips_to_ban_proper));
					} else {
						printOut(FAILURE, sprintf(L_COMMENT_IPS_NOT_BANNED, $ips_to_ban_proper));
					}
				}
				}
}		
if(!empty($item_id)){
				$comment =& $comments;
				if (is_posting(L_BUTTON_EDIT)) {
										if(saveItem($item_id, 'comments', $_POST)){
										$comment->Get($item_id);
											printOut(SUCCESS, vsprintf(L_EDIT_SUCCESS, $comment->name));
										} else {
										$comment->Get($item_id);
											printOut(FAILURE,vsprintf(L_EDIT_FAILURE, $comment->name));
										}
										 }
									
									$comment->Get($item_id);	 
									$hasitems = (empty($comment->id)) ? 0 : 1;
				?> 
					<?php echo $output;?> 
<?php 
if($hasitems){
?>
		<form action="" method="post">
<label for="online"><?php echo L_COMMENT_ONLINE ?></label>
<input name="online" type="hidden" id="" value="0" />
<input class="cBox" type="checkbox" name="online" value="1" id="online" <?php echo ($comment->online == 1) ? 'checked="checked"' : '';?> />
<br />
<label for="name"><?php echo L_COMMENT_NAME ?></label><input name="name" id="name" class="formfields" type="text" value="<?php echo $comment->name ?>" />
<input type="hidden" name="itemid" value="<?php echo $comment->itemid ?>" />
<input type="hidden" name="cid" value="<?php echo $comment->cid ?>" />
<br />
<label for="email"><?php echo L_COMMENT_EMAIL ?></label><input name="email" id="email" type="text" class="formfields" value="<?php echo $comment->email ?>" /><br />
<label for="url"><?php echo L_URL ?></label><input name="url" id="url" type="text" class="formfields" value="<?php echo $comment->url ?>" /><br />
<label for="message"><?php echo L_COMMENT_COMMENTS ?></label><textarea name="message" rows="" cols="" id="message" class="formfields"><?php echo $comment->message ?></textarea><br />
<input type="hidden" name="del[]" value="<?php echo $comment->id ?>" />
<?php applyOzoneAction('manage_comment', $comment); ?>
<label for="edit"></label>
<input type="submit" name="submit" value="<?php echo L_BUTTON_EDIT ?>" class="buttons" />
<input type="submit" name="submit" value="<?php echo L_BUTTON_DELETE ?>" class="buttons" /><br />


</form>	
		<?php 
		}// end has items
		elseif(!$hasitems && is_posting(L_BUTTON_DELETE)) {
		printf(FAILURE, L_COMMENT_DOES_NOT_EXIST);
		}
				} if(empty($item_id)) {
		

$commentsList = $comments->GetList(array(array('itemid','>',0)));
$hasitems = empty($commentsList) ? 0 : 1;
unset($commentsList);
if(!$hasitems){
printOut(FAILURE, L_NO_COMMENTS);
}
echo $output ;
				if($hasitems){ 
				$timeformat = getOption('timeformat');
				$dateformat = getOption('dateformat');
				$itemList = $items->GetList(array(array('id','>','0')), 'id', false);
				foreach($itemList as $item){
							$comment = $comments->GetList(array(array('itemid','=',$item->id)), 'id', false);
							if(!empty($comment)){?>
								
								
								<div class="commentBlock"><h3><a href="index.php?type=edit&amp;cat_id=<?php echo $item->pid ?>&amp;id=<?php echo $item->id ?>"><?php echo $item->title; ?></a></h3><table>
			
				<?php foreach($comment as $ind => $comm): 
				$comm->name = trim_title($comm->name,L_COMMENT_NAME_MISSING);
				$comm->message = trim_excerpt($comm->message, L_COMMENT_MESSAGE_MISSING);		
				?>
						<tr<?php echo ($ind % 2) ? ' class="altRow"' : ''; ?>>
						<td><h1><?php printf(L_COMMENT_POSTED_BY,'<a href="'.$_SERVER['REQUEST_URI'].'&amp;id='.$comm->id.'">'.$comm->name.'</a>') ?> </h1>
<p><?php echo $comm->message;?></p><p><strong><?php echo L_BUTTON_DELETE ?></strong> <?php echo userDate($comm->created); ?></p></td>
						<td><a href="<?php echo $_SERVER['REQUEST_URI'] ;?>&amp;id=<?php echo $comm->id; ?>" title="<?php echo L_COMMENT_EDIT ?>" class="editLink"><?php echo L_COMMENT_EDIT ?></a></td>
						<input id="ip<?php echo $comm->id; ?>" name="ip[]" type="hidden" value="<?php echo $comm->ip; ?>" />
						<td><input id="del<?php echo $comm->id; ?>" name="del[]" type="checkbox" value="<?php echo $comm->id; ?>" /></td>
						</tr>
				<?php endforeach; ?>
			</table></div>
								
								<?php
							}
							
							 }
							  ?>
<input type="checkbox" id="ban_delete" name="ban_delete" class="buttons" value="1" />
<label for="ban_delete" class="extendedLabels"><?php echo L_BAN_DELETE ?></label>							  
<br />	
<label for="delete"></label>
<input type="submit" name="submit" id="submit" class="buttons" value="<?php echo L_BUTTON_DELETE ?>" />
<?php } 
	}
}
?>